package cn.virens.library.widget.adapter;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;

/**
 * 通用分页/下拉刷新RecyclerView数据源适配器
 *
 * @author :yyzh
 * @package :cn.loioi.widget.adapter
 * @创建时间 :2016/5/6 10:10
 */

public abstract class SimplePageAdapter<T> extends AbstractAdapter<T> implements SwipeRefreshLayout.OnRefreshListener {
    private static final int SURPLUS_ITEM_COUNT = 4;//表示剩下4个item自动加载
    private int pages = 1;// 总页数
    private int pageNum = 1;// 当前页
    private boolean busyFlag = false;// 是否忙碌中...

    private RecyclerView mRecyclerView;
    private SwipeRefreshLayout mSwipeRefreshLayout;

    private OnLoadPageListener<T> listener;

    public SimplePageAdapter(int layoutID) {
        super(layoutID);
    }

    /**
     * 设置 - 列表
     *
     * @param mRecyclerView
     */
    public void setRecyclerView(RecyclerView mRecyclerView) {
        if (mRecyclerView == null) return;

        this.mRecyclerView = mRecyclerView;
        this.mRecyclerView.setAdapter(this);
        this.mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                // 是不是忙碌 || 页码超出 || 不是上拉
                if (SimplePageAdapter.this.busyFlag || pageNum >= pages || dy < 0) return;

                RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager(); // 获取布局管理器

                // 获取显示中的最后一条的计数
                int lastVisibleItemPosition = 0;
                if (layoutManager instanceof GridLayoutManager) {
                    lastVisibleItemPosition = ((GridLayoutManager) layoutManager).findLastVisibleItemPosition();

                } else if (layoutManager instanceof LinearLayoutManager) {
                    lastVisibleItemPosition = ((LinearLayoutManager) layoutManager).findLastVisibleItemPosition();

                } else if (layoutManager instanceof StaggeredGridLayoutManager) {
                } else {

                }
                // 判断是不是显示到加载阀值
                if (lastVisibleItemPosition >= (getItemCount() - SURPLUS_ITEM_COUNT)) {
                    SimplePageAdapter.this.busyFlag = true;
                    SimplePageAdapter.this._loadPage(pageNum + 1);
                }
            }
        });
    }

    /**
     * 设置 - 下拉刷新控件
     *
     * @param mSwipeRefreshLayout
     */
    public void setSwipeRefreshLayout(SwipeRefreshLayout mSwipeRefreshLayout) {
        if (mSwipeRefreshLayout == null) return;

        this.mSwipeRefreshLayout = mSwipeRefreshLayout;
        this.mSwipeRefreshLayout.setOnRefreshListener(this);
    }

    @Override
    public void onRefresh() {
        if (busyFlag) {
            this.mSwipeRefreshLayout.setRefreshing(false);
        } else {
            this.busyFlag = true;
            this.mSwipeRefreshLayout.setRefreshing(true);

            this._loadPage(1);
        }
    }

    /**
     * 直接进行刷新操作
     */
    public void refresh() {
        this.clear();
        this.onRefresh();
    }

    /**
     * 重置忙碌状态 - 数据，在加载完成后必须调用
     *
     * @param pageNum
     * @param pages
     */
    public void restBusy(int pageNum, int pages) {
        this.setPageNum(pageNum);
        this.setPages(pages);

        this.busyFlag = false;

        if (mSwipeRefreshLayout != null) mSwipeRefreshLayout.setRefreshing(false);
    }

    /**
     * 获取 - 当前页码
     *
     * @return
     */
    public int getPageNum() {
        return pageNum;
    }

    /**
     * 设置 - 当前页码
     *
     * @param pageNum
     */
    public void setPageNum(int pageNum) {
        this.pageNum = pageNum;
    }

    /**
     * 获取 - 总页码数
     *
     * @return
     */
    public int getPages() {
        return pages;
    }

    /**
     * 设置 - 总页码数
     *
     * @param pages
     */
    public void setPages(int pages) {
        this.pages = pages;
    }

    /**
     * 获取 - 加载事件
     *
     * @return
     */
    public OnLoadPageListener<T> getOnLoadPageListener() {
        return listener;
    }

    /**
     * 设置 - 加载事件
     *
     * @param listener
     */
    public void setOnLoadPageListener(OnLoadPageListener<T> listener) {
        this.listener = listener;
    }

    /**
     * 加载一页
     *
     * @param pageNo
     */
    protected void _loadPage(int pageNo) {
        if (pageNo == 1) clear();//如果为第一页，下拉刷新显示、清空已有的数据

        if (listener != null) {
            this.listener.loadPage(pageNo);
        } else {
            this.restBusy(pageNum, pages);
        }
    }

    public interface OnLoadPageListener<E> {
        /**
         * 在加载完成后，必须调用 restBusy 以重置忙碌状态
         *
         * @param pageNo
         */
        void loadPage(int pageNo);
    }
}